package org.lx.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.lx.common.utils.DateUtils;
import org.lx.common.utils.SecurityUtils;
import org.lx.common.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.lx.nursing.mapper.AlertDataMapper;
import org.lx.nursing.domain.AlertData;
import org.lx.nursing.service.IAlertDataService;

/**
 * 报警数据Service业务层处理
 *
 * @author sewen
 * @date 2025-03-12
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 处理设备报警数据
     * @param id
     * @param dealTime
     * @param dealResult
     * @return
     */
    @Override
    public Integer handleAlertData(Long id, LocalDateTime dealTime, String dealResult) {
        //获取本地线程登录的用户id
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        UpdateWrapper<AlertData> alertDataUpdateWrapper = new UpdateWrapper<>();
        alertDataUpdateWrapper.eq("id", id)
                .set("processing_time", dealTime)
                .set("processing_result", dealResult)
                .set("update_time", DateUtils.getNowDate())
                .set("processor_name", userName)
                .set("processor_id", userId)
                .set("update_by", userId)
                .set("status", 1);
        return update(alertDataUpdateWrapper) ? 1 : 0;
    }
}
